package com.igirl.manage.service.item;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.abel533.mapper.Mapper;
import com.igirl.common.bean.ItemCatData;
import com.igirl.common.bean.ItemCatData2;
import com.igirl.common.bean.ItemCatResult;
import com.igirl.common.service.RedisService;
import com.igirl.manage.mapper.item.ItemCatMapper;
import com.igirl.manage.mapper.item.ItemMapper;
import com.igirl.manage.pojo.Item;
import com.igirl.manage.pojo.ItemCat;
import com.igirl.manage.service.BaseService;
import com.igirl.manage.service.content.PropertiesService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author: jiaheng
 * @ Description:
 * @ Date: Created in 20:09 2017/12/22
 * @ Modified:
 */
@Service
public class ItemCatService extends BaseService<ItemCat> {

    private Logger logger = LoggerFactory.getLogger(ItemCatService.class);

    @Autowired
    private RedisService redisService;

    public ItemCatResult queryAllToTree() {
        //首先从redis获取
        try {
            String json = redisService.get(PropertiesService.TT_MANAGE_ITEMCAT_CACHE);
            if (json != null && StringUtils.isNotEmpty(json)) {
               return PropertiesService.MAPPER.readValue(json, ItemCatResult.class);
            }
        } catch (IOException e) {
            logger.error("从redis获取数据出错");
            e.printStackTrace();
        }
        //查询所有数据
        List<ItemCat> itemCats = super.queryAll();

        //转为map存储，key为父节点id,value为数据集合
        Map<Long, List<ItemCat>> itemCatMap = new HashMap<>();
        for (ItemCat itemCat : itemCats) {
            if (!itemCatMap.containsKey(itemCat.getParentId())) {
                List<ItemCat> list = new ArrayList<>();
                itemCatMap.put(itemCat.getParentId(), list);
            }
            itemCatMap.get(itemCat.getParentId()).add(itemCat);
        }

        //新建一级数据集合
        List<ItemCatData> oneResults = new ArrayList<>();

        //获取最顶级数据
        List<ItemCat> oneitemCatList = itemCatMap.get(0L);

        //向一级数据装数据
        for (ItemCat itemCat : oneitemCatList) {
            ItemCatData oneResult = new ItemCatData();
            oneResult.setUrl("/products/" + itemCat.getId() + ".html");
            oneResult.setName("<a href='/products/" + itemCat.getId() + ".html'>" + itemCat.getText() + "</a>");
            //创建二级数据并封装到一级数据i,一级数据结构创建结束
            List<ItemCatData2> twoResults = new ArrayList<>();
            oneResult.setItemCatDatas(twoResults);

            //获取二级数据
            List<ItemCat> twoitemCatList = itemCatMap.get(itemCat.getId());
            for (ItemCat twoitemCat : twoitemCatList) {
                ItemCatData2 twoResult = new ItemCatData2();
                twoResult.setUrl("/products/" + twoitemCat.getId() + ".html");
                twoResult.setName("<a href='/products/" + twoitemCat.getId() + ".html'>" + twoitemCat.getText() + "</a>");
                //创建二级的i数据并封装到二级数据，二级数据结构创建结束
                List<String> threeitemList = new ArrayList<>();
                twoResult.setItemCatStr(threeitemList);
                //获取二级数据的 i 源数据
                List<ItemCat> threeitemCatList = itemCatMap.get(twoitemCat.getId());
                //创建二级数据的 i 封装数据
                for (ItemCat threeitem : threeitemCatList) {
                    threeitemList.add("/products/" + threeitem.getId() + ".html|" + threeitem.getName());
                }
                //将一个封装好的二级数据放到一级数据的i数据
                twoResults.add(twoResult);
            }

            //将一个一级数据封装到一级数据数据集合
            oneResults.add(oneResult);
        }
        ItemCatResult itemCatResult = new ItemCatResult();
        itemCatResult.setItemCats(oneResults);
        //将获取到的数据保存到redis中
        try {
            String value = PropertiesService.MAPPER.writeValueAsString(itemCatResult);
            redisService.setex(PropertiesService.TT_MANAGE_ITEMCAT_CACHE,value,60*60*60*24*7);
        } catch (JsonProcessingException e) {
            logger.error("将数据保存到redis出错");
            e.printStackTrace();
        }
        return itemCatResult;
    }
}
